/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.univcasa.managing.impl;

import com.univcasa.db.model.CompteEtudiant;
import com.univcasa.db.model.Criteres;
import com.univcasa.db.persist.HibernateUtil;
import com.univcasa.managing.ComptesStudentsManager;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author issam
 */
public class ComptesStudentsManagerImpl implements ComptesStudentsManager {

    private static Logger log = Logger.getLogger(ComptesStudentsManagerImpl.class);

    public boolean isExists(CompteEtudiant compte){

        String apogee = compte.getApogee();
        CompteEtudiant std = getCompteEtudiantByApogee(apogee);
        if (std==null){
            return false;
        }
        else {
            return true;
        }
    }    

    public CompteEtudiant getCompteEtudiantByApogee(String apogee){
        
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "Select etudiant From CompteEtudiant etudiant where  etudiant.apogee like :apogee" ;
        Query query = session.createQuery( hql );
        query.setString("apogee", apogee );
        query.setMaxResults(1);
        CompteEtudiant etudiant = (CompteEtudiant) query.uniqueResult();
        return etudiant;
    }
    /*@TODO ajout de criteres cin apogee cne dans la requete
     * apres resolution du like %
     *
     * */

    public CompteEtudiant getCompteEtudiantByCriteria(Criteres criteria){

        String apogee = criteria.getCritere("apogee");
        String cne = criteria.getCritere("cne");
        String nom = criteria.getCritere("nom");
        String prenom = criteria.getCritere("prenom");
        String cin = criteria.getCritere("cin");
        String etab = criteria.getCritere("etab");

        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "Select etudiant From CompteEtudiant etudiant where etudiant.cne like :cne  and etudiant.apogee like :apogee and etudiant.cin like :cin and etudiant.nom like :nom and etudiant.prenom like :prenom and etab like :etab" ;
        Query query = session.createQuery( hql );
        query.setString("cne", cne );
        query.setString("apogee", apogee );
        query.setString("nom", nom );
        query.setString("prenom", prenom );
        query.setString("cin", cin );
        query.setString("etab", etab );
        query.setMaxResults(1);
        CompteEtudiant etudiant = (CompteEtudiant) query.uniqueResult();
        return etudiant;
    }

    public void ajouterCompteEtudiant(CompteEtudiant etudiant) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        session.save(etudiant);
        tx.commit();
        log.debug("SUCCESS ADD Student to DB " + etudiant.getApogee() );
    }

    public void ajouterComptesEtudiants(List comptes){


        Session session = HibernateUtil.getSessionFactory().openSession();

        for (Iterator itr = comptes.iterator(); itr.hasNext(); ){
            Transaction tx = session.beginTransaction();
            CompteEtudiant etudiant = (CompteEtudiant) itr.next();
            session.save(etudiant);
            tx.commit();
            log.debug("SUCCESS ADD Student to DB " + etudiant.getApogee() );
        }
    }    

    public void majCompteEtudiant(CompteEtudiant etudiant){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        session.merge(etudiant);
        tx.commit();
        log.debug("SUCCESS MAJ Student to DB " + etudiant.getApogee() + etudiant.getUid() + etudiant.getCin() );
    }

    public void majComptesEtudiants(List comptes){
        Session session = HibernateUtil.getSessionFactory().openSession();
        for (Iterator itr = comptes.iterator(); itr.hasNext(); ){
            CompteEtudiant etudiant = (CompteEtudiant) itr.next();
            Transaction tx = session.beginTransaction();
            session.merge(etudiant);
            tx.commit();
            log.debug("SUCCESS MAJ Student to DB " + etudiant.getApogee() );
        }
    }

    public List getApogeeComptesStudents(Criteres criteria){

        String apogee = (String) criteria.get("apogee");
        String cne = (String) criteria.get("cne");
        String etab = (String) criteria.get("etab");

        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        String hql = "SELECT etudiant.apogee FROM CompteEtudiant etudiant WHERE (etudiant.cne like :cne or etudiant.cne=null) and etudiant.apogee like :apogee and etudiant.etab like :etab " ;
        Query query = session.createQuery( hql );
        query.setString("cne", cne );
        query.setString("apogee", apogee );
        query.setString("etab", etab );
        List lista = query.list();

        return lista;
    }

    public List getListComptesStudentsByCriteria(Criteres criteria) {

        String apogee =  criteria.getCritere("apogee");
        String cne =  criteria.getCritere("cne");
        String nom =  criteria.getCritere("nom");
        String prenom =  criteria.getCritere("prenom");
        String cin =  criteria.getCritere("cin");

        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "Select etudiant From CompteEtudiant etudiant where etudiant.cne like :cne and etudiant.apogee like :apogee and etudiant.cin like :cin and etudiant.nom like :nom and etudiant.prenom like :prenom " ;
        Query query = session.createQuery( hql );
        query.setString("cne", cne );
        query.setString("apogee", apogee );
        query.setString("nom", nom );
        query.setString("prenom", prenom );
        query.setString("cin", cin );
        List res = query.list();

        return res;
    }
}
